//
//  ChooseProSearchVC.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2024/11/5.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
class ChooseProSearchVC: ZyaChooseProductVC {
    private var searchBar:UISearchBar?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.mj_header = KSRefreshHeader {
            self.page = 1
            self.network()
        }
   
        self.tableView.emptyDataSetSource = self
        self.tableView.emptyDataSetDelegate = self
        self.configSearch()
    }
    override func network() {
        let api = API.pFoodQueryGoodsBySearch(page: "\(page)", size: "100", s_title: s_title, s_org_id: s_orgid, s_client_id: clientid)
        KSNetwork.shared.doRequest(api,showHud: false) { (data:BaseResult<ZyaFOGoodTModel>) in
            if data.flag == TRUE {
                if let infos = data.infoArrCombine {
                    let newInfos = infos.compactMap { one -> ZyaFOGoodTModel in
                        let nn = one
                        return nn
                    }
                    if self.page == 1 {
                        self.markSearchArray.removeAll()
                    }
                    self.markSearchArray += newInfos
                    self.tableView.reloadData()
                    
                    
                }
            } else {
                KSToast(data.errorCombine.unwrap)
            }
            
            self.tableView.mj_header?.endRefreshing()
            
        }
    }
    func configSearch () {
        
        searchBar = TenderSearchBar(frame:CGRect(x:0,y:3,width:KSGlobal.screenW,height:40))
        if #available(iOS 13.0, *) {
            searchBar!.searchTextField.backgroundColor = .white
            searchBar!.searchTextField.layer.cornerRadius = 18
            searchBar!.searchTextField.layer.masksToBounds = true
        } else {
            // Fallback on earlier versions
            let searchField = searchBar!.value(forKey: "searchField") as! UITextField
            searchField.backgroundColor = .white
            searchField.layer.cornerRadius = 18
            searchField.layer.masksToBounds = true
            
        }
        searchBar?.barTintColor = UIColor(kshex: "#f5f5f5")
        searchBar?.backgroundImage = UIImage()
        
        let toolbar = UIToolbar()
        toolbar.barStyle = UIBarStyle.default
        toolbar.sizeToFit()
        let buttonflexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let buttonDone = UIBarButtonItem(title: "完成", style: UIBarButtonItem.Style.done, target: self, action: #selector(doneClickss))
        buttonDone.tintColor = UIColor.black
        toolbar.setItems([buttonflexible, buttonDone], animated: true)
        
        searchBar!.inputAccessoryView = toolbar
        
        
        
        searchBar!.text = self.s_title
        searchBar!.placeholder = "请输入关键字"
        searchBar!.delegate = self
        self.tableView.tableHeaderView = searchBar!
        
        
    }
    @objc func doneClickss() {
        self.searchBar!.resignFirstResponder()
    }
    
    
    override  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: ZyaChooseProCell.identifier, for: indexPath) as? ZyaChooseProCell else {
            fatalError()
        }
        
        cell.setLastNodeType(with: self.markSearchArray[indexPath.row])
        
        
        
        // 点击收藏 事件
        cell.markClosure = { [indexPath] in
            
            let sm = self.markSearchArray[indexPath.row].sMark.unwrap
            self.markSearchArray[indexPath.row].sMark = (sm == "1") ? "0" : "1"
            self.setMarkNetwork(id: self.markSearchArray[indexPath.row].sGoodsId.unwrap, mark: self.markSearchArray[indexPath.row].sMark.unwrap)
            self.tableView.reloadData()
            
        }
        
        return cell
    }
    override  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.markSearchArray.count
        
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
        self.markSearchArray[indexPath.row].selectX =  !self.markSearchArray[indexPath.row].selectX
        self.tableView.reloadData()
        
        
        
    }
}
